//
//  RoomSettingViewController.m
//  OutesIOS
//
//  Created by norman on 2018/12/3.
//  Copyright © 2018 norman. All rights reserved.
//

#import "RoomSettingViewController.h"
#import "TPKeyboardAvoidingTableView.h"
#import "EditorTableViewCell.h"
#import "HttpTool.h"

@interface RoomSettingViewController ()

@property (nonatomic,strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic,strong) NSMutableDictionary *settingData;

@end

@implementation RoomSettingViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"室内设计设置";
    [self setupViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self updateSetting];
}

- (void)setupViews{
    //列表
    self.tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.backgroundColor = CM_TABLE_BG_COLOR;
    [self.tableView registerClass:[EditorTableViewCell class] forCellReuseIdentifier:@"edit"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //去掉多余的分割线
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
    
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)loadData {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:[AccountManager manager].token forKey:@"token"];
    [HttpTool get:HTTP_GET_SETTING_INFO params:param success:^(NSDictionary *responseObj) {
        [self.tableView.mj_header endRefreshing];
        self.settingData = [responseObj mutableCopy];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)updateSetting {
    //参数
    if (self.settingData == nil) {
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:[self.settingData objectForKey:@"cold_dry_bulb_t"] forKey:@"cold_dry_bulb_t"];
//    [params setValue:[self.settingData objectForKey:@"cold_wet_bulb_t"] forKey:@"cold_wet_bulb_t"];
    [params setValue:[self.settingData objectForKey:@"relative_humidity"] forKey:@"relative_humidity"];
    [params setValue:[self.settingData objectForKey:@"heat_design_t"] forKey:@"heat_design_t"];
    
    [HttpClient postWithUrl:HTTP_POST_SETTING_ROOM token:[AccountManager manager].token paramDic:params resultClass:[CommonResponse class] success:^(CommonResponse *obj) {
    } failure:^(NSError *error) {
    }];
}

#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"edit"];
    [cell setKeyboardType:UIKeyboardTypeDecimalPad];
    if (indexPath.row == 0) {
        [cell setTitleStr:@"制冷设计干球温度" andColor:nil andHintStr:@"请输入" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"℃"];
        if ([[self.settingData objectForKey:@"cold_dry_bulb_t"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"cold_dry_bulb_t"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"cold_dry_bulb_t"];
        };
        return cell;
    }
//    else if (indexPath.row == 1) {
//        [cell setTitleStr:@"制冷设计湿球温度" andColor:nil andHintStr:@"请输入" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"℃"];
//        if ([[self.settingData objectForKey:@"cold_wet_bulb_t"] doubleValue] != 0) {
//            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"cold_wet_bulb_t"] doubleValue]]];
//        }
//        cell.TextFiledValueBlock = ^(NSString *value) {
//            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"cold_wet_bulb_t"];
//        };
//        return cell;
//    }
    else if (indexPath.row == 1) {
        [cell setTitleStr:@"制冷设计相对湿度" andColor:nil andHintStr:@"请输入" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"%"];
        if ([[self.settingData objectForKey:@"relative_humidity"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%d", (int)([[self.settingData objectForKey:@"relative_humidity"] doubleValue]*100)]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            if ([value intValue] >= 1 && [value intValue] <= 100) {
                double v = [value doubleValue]/100.0;
                [self.settingData setValue:[NSNumber numberWithDouble:v] forKey:@"relative_humidity"];
            }
        };
        cell.TextFiledEditingEndBlock = ^(NSString *value) {
            if ([value intValue] < 1 || [value intValue] > 100) {
                [Tools showMessageCenter:@"制冷设计相对湿度输入错误"];
            }
        };
        return cell;
    } else {
        [cell setTitleStr:@"制热设计温度" andColor:nil andHintStr:@"请输入" andInputViewIsHidden:NO andInfoStr:nil andUnit:@"℃"];
        if ([[self.settingData objectForKey:@"heat_design_t"] doubleValue] != 0) {
            [cell setEditorText:[NSString stringWithFormat:@"%0.1f", [[self.settingData objectForKey:@"heat_design_t"] doubleValue]]];
        }
        cell.TextFiledValueBlock = ^(NSString *value) {
            [self.settingData setValue:[NSNumber numberWithDouble:[value doubleValue]] forKey:@"heat_design_t"];
        };
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell1";
    EditorTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[EditorTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    return [cell getHeight];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, DEFAULT_SECTION_HEIGHT)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return DEFAULT_SECTION_HEIGHT;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, CGFLOAT_MIN)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
